package cn.mooncode.basketballManage.controller;


import cn.mooncode.basketballManage.pojo.RespBean;
import cn.mooncode.basketballManage.pojo.RespPageBean;
import cn.mooncode.basketballManage.pojo.Space;
import cn.mooncode.basketballManage.service.ISpaceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jwz
 * @since 2021-03-28
 */
@RestController
@RequestMapping("/space")
public class SpaceController {

    @Autowired
    private ISpaceService spaceService;

    @ApiOperation(value = "获取所有场地信息")
    @GetMapping("/")
    public RespPageBean getAllSpaces(@RequestParam(defaultValue = "1") Integer currentPage,
                                     @RequestParam(defaultValue = "10") Integer size, Space space) {
        return spaceService.getAllSpaces(currentPage, size, space);
    }

    @ApiOperation(value = "获取可使用的场地信息")
    @GetMapping("/selectedSpace")
    public RespBean getSelectedSpace() {
        return spaceService.getSelectedSpace();
    }

    @ApiOperation(value = "根据ID查询场地信息")
    @PostMapping("/spaceById")
    public RespBean getSpaceById(@RequestBody List<Integer> spaceIdList) {
        List<Space> spaces = spaceService.listByIds(spaceIdList);
        if (spaces.size() > 0) {
            return RespBean.success("查询场地信息成功！", spaces);
        }
        return RespBean.error("没有查到相关场地信息！");
    }

    @ApiOperation(value = "添加场地信息")
    @PostMapping("/")
    public RespBean addSpace(@RequestBody Space space) {
        if (spaceService.save(space)) {
            return RespBean.success("添加成功！");
        }
        return RespBean.error("添加失败！");
    }

    @ApiOperation(value = "修改场地信息")
    @PutMapping("/")
    public RespBean updateSpace(@RequestBody Space space) {
        if (spaceService.updateById(space)) {
            return RespBean.success("修改成功！");
        }
        return RespBean.error("修改失败！");
    }

    @ApiOperation(value = "修改容纳球队数目")
    @PutMapping("/teamNum")
    public RespBean updateTeamNum(Integer sid, Integer num) {
        return spaceService.updateTeamNum(sid, num);
    }

    @ApiOperation(value = "删除场地信息")
    @DeleteMapping("/{id}")
    public RespBean deleteSpace(@PathVariable Integer id) {
        if (spaceService.removeById(id)) {
            return RespBean.success("删除成功！");
        }
        return RespBean.error("删除失败！");
    }
}
